// Don't forget to make the changes mentioned in
// https://github.com/bitsdojo/bitsdojo_window#getting-started

import 'package:flutter/material.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';

/// 程序入口点
///
/// 初始化窗口大小并启动Flutter应用
/// 使用doWhenWindowReady确保窗口正确初始化
void main() {
  appWindow.size = const Size(600, 450);
  runApp(const MyApp());
  appWindow.show();
  doWhenWindowReady(() {
    final win = appWindow;
    const initialSize = Size(600, 450);
    win.minSize = initialSize;
    win.size = initialSize;
    win.alignment = Alignment.center;
    win.title = "Custom window with Flutter";
    win.show();
  });
}

/// 边框颜色常量
const borderColor = Color(0xFF805306);

/// 应用根组件
///
/// 构建带有自定义边框的主界面
class MyApp extends StatelessWidget {
  /// 创建MyApp实例
  ///
  /// [key] - 组件的键值
  const MyApp({super.key});

  @override
  /// 构建应用的UI界面
  ///
  /// [context] - 构建上下文
  /// 返回一个去除调试横幅的MaterialApp组件
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: WindowBorder(
          color: borderColor,
          width: 1,
          child: const Row(
            children: [LeftSide(), RightSide()],
          ),
        ),
      ),
    );
  }
}

/// 侧边栏颜色常量
const sidebarColor = Color(0x4D000000);

/// 左侧边栏组件
///
/// 显示固定宽度的侧边栏区域
class LeftSide extends StatelessWidget {
  /// 创建LeftSide实例
  ///
  /// [key] - 组件的键值
  const LeftSide({super.key});

  @override
  /// 构建左侧边栏UI
  ///
  /// [context] - 构建上下文
  /// 返回一个包含窗口标题栏和可扩展区域的组件
  Widget build(BuildContext context) {
    return SizedBox(
        width: 200,
        child: Container(
            color: sidebarColor,
            child: Column(
              children: [
                WindowTitleBarBox(child: MoveWindow()),
                Expanded(child: Container())
              ],
            )));
  }
}

/// 背景渐变起始颜色
const backgroundStartColor = Color(0xFFFFD500);

/// 背景渐变结束颜色
const backgroundEndColor = Color(0xFFF6A00C);

/// 右侧主内容区域组件
///
/// 显示带有渐变背景的主内容区域
class RightSide extends StatelessWidget {
  /// 创建RightSide实例
  ///
  /// [key] - 组件的键值
  const RightSide({super.key});

  @override
  /// 构建右侧内容区域UI
  ///
  /// [context] - 构建上下文
  /// 返回一个带有渐变背景和窗口控制按钮的内容区域
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [backgroundStartColor, backgroundEndColor],
              stops: [0.0, 1.0]),
        ),
        child: Column(children: [
          WindowTitleBarBox(
            child: Row(
              children: [Expanded(child: MoveWindow()), const WindowButtons()],
            ),
          )
        ]),
      ),
    );
  }
}

/// 窗口按钮通用颜色配置
final buttonColors = WindowButtonColors(
    iconNormal: const Color(0xFF805306),
    mouseOver: const Color(0xFFF6A00C),
    mouseDown: const Color(0xFF805306),
    iconMouseOver: const Color(0xFF805306),
    iconMouseDown: const Color(0xFFFFD500));

/// 关闭按钮颜色配置
final closeButtonColors = WindowButtonColors(
    mouseOver: const Color(0xFFD32F2F),
    mouseDown: const Color(0xFFB71C1C),
    iconNormal: const Color(0xFF805306),
    iconMouseOver: Colors.white);

/// 窗口控制按钮组件
///
/// 显示窗口最小化、最大化/还原和关闭按钮
class WindowButtons extends StatefulWidget {
  /// 创建WindowButtons实例
  ///
  /// [key] - 组件的键值
  const WindowButtons({super.key});

  @override
  /// 创建窗口按钮的状态对象
  ///
  /// 返回_WindowButtonsState实例
  State<WindowButtons> createState() => _WindowButtonsState();
}

/// WindowButtons组件的状态类
class _WindowButtonsState extends State<WindowButtons> {
  /// 切换窗口最大化/还原状态
  ///
  /// 根据当前窗口状态切换最大化或还原窗口
  void maximizeOrRestore() {
    setState(() {
      appWindow.maximizeOrRestore();
    });
  }

  @override
  /// 构建窗口控制按钮UI
  ///
  /// [context] - 构建上下文
  /// 返回包含最小化、最大化/还原和关闭按钮的行布局
  Widget build(BuildContext context) {
    return Row(
      children: [
        MinimizeWindowButton(colors: buttonColors),
        appWindow.isMaximized
            ? RestoreWindowButton(
          colors: buttonColors,
          onPressed: maximizeOrRestore,
        )
            : MaximizeWindowButton(
          colors: buttonColors,
          onPressed: maximizeOrRestore,
        ),
        CloseWindowButton(colors: closeButtonColors),
      ],
    );
  }
}
